//
//  JrdDatamanager.m
//  v2ex-iOS
//
//  Created by longcheng on 16/6/14.
//  Copyright © 2016年 Singro. All rights reserved.
//

#import "JrdDatamanager.h"
static NSString *const kUsername = @"username";
static NSString *const kUserid = @"userid";
static NSString *const kPassword = @"password";
static NSString *const kUserphoto = @"userphoto";
static NSString *const kUserEmail = @"userEmail";
static NSString *const kUserPhone = @"userPhone";

static NSString *const kAvatarURL = @"avatarURL";
static NSString *const kUserIsLogin = @"userIsLogin";

@implementation JrdDatamanager
- (instancetype)init {
    if (self = [super init]) {
        BOOL isLogin = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] boolValue];
        if (isLogin) {
            JrdUserModel *user = [[JrdUserModel alloc] init];
            user.userId = [[NSUserDefaults standardUserDefaults] objectForKey:kUserid];
            user.userName = [[NSUserDefaults standardUserDefaults] objectForKey:kUsername];
            user.password = [[NSUserDefaults standardUserDefaults] objectForKey:kPassword];
            user.userPhoto = [[NSUserDefaults standardUserDefaults] objectForKey:kUserphoto];
            user.userEamil = [[NSUserDefaults standardUserDefaults] objectForKey:kUserEmail];
            user.phoneNumber = [[NSUserDefaults standardUserDefaults] objectForKey:kUserPhone];

            _user = user;
        }
        
    }
    return self;
}

- (void)setUser:(JrdUserModel *)user {
    _user = user;
    
    if (user) {        
        [[NSUserDefaults standardUserDefaults] setObject:user.userName forKey:kUsername];
        [[NSUserDefaults standardUserDefaults] setObject:user.userId forKey:kUserid];
        [[NSUserDefaults standardUserDefaults] setObject:user.userPhoto forKey:kUserphoto];
        [[NSUserDefaults standardUserDefaults] setObject:user.password forKey:kPassword];
        [[NSUserDefaults standardUserDefaults] setObject:user.userEamil forKey:kUserEmail];
        [[NSUserDefaults standardUserDefaults] setObject:user.phoneNumber forKey:kUserPhone];

        [[NSUserDefaults standardUserDefaults] synchronize];
    } else {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUsername];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUserid];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUserphoto];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kPassword];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUserPhone];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUserEmail];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (void)setUserEmail:(NSString*)email{
    [[NSUserDefaults standardUserDefaults] setObject:email forKey:kUserEmail];
}

- (void)setUserPhone:(NSString*)phone{
    [[NSUserDefaults standardUserDefaults] setObject:phone forKey:kUserPhone];
}


- (void)setUserPassword:(NSString*)password{
    [[NSUserDefaults standardUserDefaults] setObject:password forKey:kPassword];
}

- (int)getCheckValue{
    int checkValue_1 = 0;
    NSString* checkValue = [[NSUserDefaults standardUserDefaults] objectForKey:kCheckValue];
    checkValue_1 = [checkValue intValue];
    return checkValue_1;
}
+ (instancetype)manager {
    static JrdDatamanager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[JrdDatamanager alloc] init];
    });
    return manager;
}



@end
